﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
using System.IO;

using DataAccessEntLib;

public partial class searchControl : System.Web.UI.UserControl
{
    BO.Ad bo = new BO.Ad();
    BLL.AdMagnager bll = new BLL.AdMagnager();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindLocation();
            BindCategory();
            BindSubCategory();
            //BindType();
        }
    }

    protected void BindCategory()
    {
        IDataReader dr;
        ddlCategory.Items.Clear();
        ListItem lst1 = new ListItem();
        lst1.Text = "Select";
        lst1.Value = "0";
        ddlCategory.Items.Add(lst1);
        dr = bll.SelectAdCategoryMaster();
        if (dr != null)
        {
            while (dr.Read())
            {
                ListItem lst = new ListItem();
                lst.Text = dr["AdCategory"].ToString();
                lst.Value = dr["AdCategoryID"].ToString();
                ddlCategory.Items.Add(lst);
            }
        }
        dr.Close();
    }

    protected void BindSubCategory()
    {
        IDataReader dr;
        ddlSubCategory.Items.Clear();
        ListItem lst1 = new ListItem();
        lst1.Text = "Select";
        lst1.Value = "0";
        ddlSubCategory.Items.Add(lst1);
        if (ddlCategory.SelectedItem.Text != "Select")
        {
            bo.AdCategoryID = Convert.ToInt32(ddlCategory.SelectedValue.ToString());
        }
        else
        {
            bo.AdCategoryID = 0;
        }
        dr = bll.ShowSubCategoryMasterDetails(bo);
        if (dr != null)
        {
            while (dr.Read())
            {
                ListItem lst = new ListItem();
                lst.Text = dr["SubCategory"].ToString();
                lst.Value = dr["SubCategoryID"].ToString();
                ddlSubCategory.Items.Add(lst);
            }
        }
        dr.Close();
    }

    protected void BindLocation()
    {
        //IDataReader dr;
        //ddlLocation.Items.Clear();
        //ListItem lst1 = new ListItem();
        //lst1.Text = "Select";
        //lst1.Value = "0";
        //ddlLocation.Items.Add(lst1);
        ////dr = bll.SelectLocationMaster();
        //dr = bll.SelectCountryMaster();
        //if (dr != null)
        //{
        //    while (dr.Read())
        //    {
        //        ListItem lst = new ListItem();
        //        //lst.Text = dr["LandMark"].ToString();
        //        lst.Text = dr["CountryName"].ToString();
        //        lst.Value = dr["ID"].ToString();
        //        ddlLocation.Items.Add(lst);
        //    }
        //}
        //dr.Close();
    }

    bool Validate()
    {
        if (AdTitle.Text == string.Empty && ddlSubCategory.SelectedItem.Text == "Select" && ddlCategory.SelectedItem.Text == "Select")
        {
            return false;
        }
        return true;
    }

    protected void btnAdvanceSearch_Click(object sender, ImageClickEventArgs e)
    {
        if (Validate())
        {
            string strSearchValue = string.Empty, ColumnName = string.Empty;
            Session["AdTitle"] = null;
            Session["CategoryID"] = null;
            Session["CategoryName"] = null;
            Session["SubCategoryID"] = null;
            Session["SubCategoryName"] = null;
            if (AdTitle.Text != string.Empty)
            {
                strSearchValue = "AdTitle" + ",";
                Session["AdTitle"] = AdTitle.Text;
            }
            //if (ddlLocation.SelectedItem.Text != "Select")
            //{
            //    strSearchValue += "Country" + ",";
            //    Session["Country"] = ddlLocation.SelectedItem.Text;
            //}
            if (ddlCategory.SelectedItem.Text != "Select")
            {
                strSearchValue += "AdCategoryID" + ",";
                Session["CategoryID"] = Convert.ToInt32(ddlCategory.SelectedValue.ToString());
                Session["CategoryName"] = ddlCategory.SelectedItem.Text;
            }
            if (ddlSubCategory.SelectedItem.Text != "Select")
            {
                strSearchValue += "SubCategoryID" + ",";
                Session["SubCategoryID"] = Convert.ToInt32(ddlSubCategory.SelectedValue.ToString());
                Session["SubCategoryName"] = ddlSubCategory.SelectedItem.Text;
            } 
            if (strSearchValue.Length > 0)
            {
                strSearchValue = strSearchValue.Substring(0, strSearchValue.LastIndexOf(","));
                Session["SearchValue"] = strSearchValue;
            }
            Response.Redirect("Cld-Search.aspx");
        }
        else
        {
            lblMessage.Text = "Please  choose atleast one parameter for search";
        }
    }

    protected void ddlCategory_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindSubCategory();
    }

}